
//
//  XQHomeView.swift
//  XQSwiftUIDemo
//
//  Created by WXQ on 2019/11/12.
//  Copyright © 2019 WXQ. All rights reserved.
//

import SwiftUI

struct XQHomeView: View {
    
    /// 一般静态数据, 用 @State, 动态用 @ObservedObject
    @ObservedObject var dataObj = XQHomeData.init(dataArr: xq_dataArr)
    
    func cellToView(_ item: XQHomeViewCellModel) -> AnyView {
        print(#function, item)
        
        switch item.type {
        case .unknow:
            return AnyView.init(Text("未知类型..."))
        case .hStack:
            return AnyView.init(XQHStackView())
        case .vStack:
            return AnyView.init(XQVStackView())
        case .zStack:
            return AnyView.init(XQZStackView())
        case .label:
            return AnyView.init(XQLabelView())
        case .button:
            return AnyView.init(XQButtonView())
        case .image:
            return AnyView.init(XQImageView())
        case .textField:
            return AnyView.init(XQTextFieldView())
        case .list:
            return AnyView.init(XQListView())
        case .slider:
            return AnyView.init(XQHStackView())
        case .alert:
            return AnyView.init(XQHStackView())
        case .webView:
            return AnyView.init(XQWebView())
        case .hvzStack:
            return AnyView.init(XQHVZStackView())
        case .xq_switch:
            return AnyView.init(XQSwitchView())
        }
        
    }
    
    var body: some View {
        
        List(self.dataObj.dataArr) { item in
            NavigationLink(destination: self.cellToView(item), label: {
                HStack {
                    Text(item.name)
                }
            })
                .isDetailLink(false)
                .deleteDisabled(item.deleteDisabled)
                .moveDisabled(item.moveDisabled)
        }
            .navigationBarTitle("UI", displayMode: .large)
    }
    
    
    
}





struct XQHomeView_Previews: PreviewProvider {
    static var previews: some View {
        XQHomeView()
    }
}

fileprivate var xq_dataArr: [XQHomeViewCellModel] = [
    XQHomeViewCellModel(name: "HStack", type: .hStack),
    XQHomeViewCellModel(name: "VStack", type: .vStack),
    XQHomeViewCellModel(name: "ZStack", type: .zStack),
    XQHomeViewCellModel(name: "HStack, VStack, ZStack 三种组合演示", type: .hvzStack),
    XQHomeViewCellModel(name: "Label", type: .label),
    XQHomeViewCellModel(name: "Button", type: .button),
    XQHomeViewCellModel(name: "Image", type: .image),
    XQHomeViewCellModel(name: "TextField", type: .textField),
    XQHomeViewCellModel(name: "List", type: .list),
    XQHomeViewCellModel(name: "Alert", type: .alert),
    XQHomeViewCellModel(name: "Slider", type: .slider),
    XQHomeViewCellModel(name: "Switch", type: .xq_switch),
    XQHomeViewCellModel(name: "WebView", type: .webView)
]



